package com.fy.fyzf;

import android.util.Log;

import com.allenliu.versionchecklib.v2.AllenVersionChecker;
import com.allenliu.versionchecklib.v2.builder.UIData;
import com.allenliu.versionchecklib.v2.callback.ForceUpdateListener;
import com.chaychan.library.BottomBarLayout;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.base.BaseFragment;
import com.fy.fyzf.bean.NewVersionBean;
import com.fy.fyzf.presenter.VersionCheckPresenter;
import com.fy.fyzf.statusbar.Eyes;
import com.fy.fyzf.utils.AppUtils;
import com.fy.fyzf.view.NoScrollViewPager;
import com.fy.fyzf.view.VersionCheckCallBack;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import ui.adapter.MainTabAdapter;
import ui.fragment.DemandFragment;
import ui.fragment.HouseResouceFragment;
import ui.fragment.PassengerSourceFragment;
import ui.fragment.SelfInfoFragment;

public class MainActivity extends BaseActivity<VersionCheckPresenter> implements VersionCheckCallBack {

    @BindView(R.id.vp_content)
    NoScrollViewPager mVpContent;
    @BindView(R.id.bottom_bar)
    BottomBarLayout bottomBar;

    @Override
    protected VersionCheckPresenter createPresenter() {
        return new VersionCheckPresenter(this);
    }

    private List<BaseFragment> fragmentList;

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        super.initView();
        Eyes.setAndroidNativeLightStatusBar(this, true);
//        Eyes.setStatusBarColor(this, Color.WHITE);

    }

    @Override
    public void initListener() {
        super.initListener();
        MainTabAdapter mTabAdapter = new MainTabAdapter(fragmentList, getSupportFragmentManager());
        mVpContent.setAdapter(mTabAdapter);
        mVpContent.setOffscreenPageLimit(fragmentList.size());
        bottomBar.setViewPager(mVpContent);
    }

    @Override
    public void initData() {
        super.initData();
        fragmentList = new ArrayList<>(3);
        fragmentList.add(new HouseResouceFragment());
        fragmentList.add(new PassengerSourceFragment());
        fragmentList.add(new DemandFragment());
        fragmentList.add(new SelfInfoFragment());
        mPresenter.getNewVersion();

    }

    @Override
    public void getNewVersionSuccess(NewVersionBean newVersionBean) {
        if (null == newVersionBean) {
            return;
        }
        String newVersion = newVersionBean.getVersion();
        String localVersion = AppUtils.getVersion(MainActivity.this);
        if (AppUtils.compareVersions(newVersion, localVersion)) {
            AllenVersionChecker
                    .getInstance()
                    .downloadOnly(
                            UIData.create().setTitle("发现新版本").setContent(newVersionBean.getVersionDesc()).setDownloadUrl(newVersionBean.getDownUrl())
                    ).setSilentDownload(true)
                    .setForceUpdateListener(new ForceUpdateListener() {
                        @Override
                        public void onShouldForceUpdate() {

                        }
                    })
                    .executeMission(MainActivity.this);
        }

    }
}